package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model, HttpServletRequest request){
        model.addAttribute("hotRegions", regionService.queryHotRegions());
        return "/destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String queryHotDestByRegionId(Long regionId, Model model){
        model.addAttribute("regionId", regionId);
        List<Destination> list = destinationService.queryHotDestByRegionId(regionId);
        List<Destination> leftDests = list.subList(0, list.size() / 2 + 1);
        model.addAttribute("leftDests", leftDests);
        List<Destination> rightDests = list.subList(list.size() / 2 + 1, list.size());
        model.addAttribute("rightDests", rightDests);
        return "/destination/hotdestTpl";
    }


    @RequestMapping("/guide")
    public String guide(Long id, Model model){
        // toasts
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        // dest
        model.addAttribute("dest", dest);

        //攻略分类 同时关联查询 攻略分类下明细
        // catalogs
        model.addAttribute("catalogs", strategyCatalogService.queryCatalogsByDestId(id));
        //前3篇  strategyDetails
        List<StrategyDetail> strategyDetails = strategyDetailService.getViewNumTop3(id);
        model.addAttribute("strategyDetails",strategyDetails);



        return "/destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(@ModelAttribute("qo")StrategyCatalogQuery qo , Model model){

        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //中国  广东
        Destination dest = toasts.remove(toasts.size() - 1);

        //toasts
        model.addAttribute("toasts", toasts);

        //dest  广州
        model.addAttribute("dest", dest);

        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo")StrategyCatalogQuery  qo ){

        //catalogs  某个目的地下的分类集合
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogsByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);

        //catalog  选中的分类
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",catalog);

        //detail  选中的分类下的第一个明细
        List<StrategyDetail> details = catalog.getDetails();
        if(details != null && details.size() >0){
            StrategyDetail detail = details.get(0);
            detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
            model.addAttribute("detail",detail);
        }

        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(@ModelAttribute("qo")TravelQuery qo, Model model){
        // pageInfo
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "destination/travelTpl";
    }


}
